package org.dromara.visitor.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.visitor.domain.dto.BlacklistReq;
import org.dromara.visitor.domain.dto.BlacklistResp;
import org.dromara.visitor.domain.entity.Blacklist;
import org.dromara.visitor.domain.entity.OrgManage;
import org.dromara.visitor.domain.entity.Organization;
import org.dromara.visitor.mapper.BlacklistMapper;
import org.dromara.visitor.mapper.OrganizationMapper;
import org.dromara.visitor.service.BlacklistService;
import org.dromara.visitor.service.OrgManageService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class BlacklistServiceImpl extends ServiceImpl<BlacklistMapper, Blacklist> implements BlacklistService {

    @Resource
    private BlacklistMapper blacklistMapper;
    @Resource
    private OrgManageService orgManageService;

    @Resource
    private OrganizationMapper organizationMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(Long id, String reason) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        OrgManage orgManage = orgManageService.getById(id);
        //需要删除原来的记录
        orgManageService.removeById(id);
        Blacklist blacklist = new Blacklist();
        blacklist.setApplyName(orgManage.getApplyName());
        blacklist.setApplyPhone(orgManage.getApplyPhone());
        blacklist.setApplyPic(orgManage.getApplyPic());
        blacklist.setCardNum(orgManage.getCardNum());
        blacklist.setReason(reason);
        blacklist.setCreateBy(loginUser.getUserId());
        blacklist.setUpdateBy(loginUser.getUserId());
        blacklist.setCreateTime(LocalDateTime.now());
        blacklist.setUpdateTime(LocalDateTime.now());
        save(blacklist);
    }

    @Override
    public Page<BlacklistResp> listPage(BlacklistReq req) {
        Page page = new Page(req.getPageNum(), req.getPageSize());
        List<BlacklistResp> list = blacklistMapper.listPage(page, req);
        page.setRecords(list);
        return page;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void backOrgManage(Long id) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        Blacklist black = this.getById(id);
        LambdaQueryWrapper<Organization> orgQueryWrapper = new LambdaQueryWrapper<>();
        orgQueryWrapper.eq(Organization::getIsFirst,1);
        Organization organization = organizationMapper.selectOne(orgQueryWrapper);
        OrgManage orgManage = new OrgManage();
        orgManage.setOrgId(organization.getId());
        orgManage.setApplyName(black.getApplyName());
        orgManage.setApplyPhone(black.getApplyPhone());
        orgManage.setApplyPic(black.getApplyPic());
        orgManage.setCardNum(black.getCardNum());
        orgManage.setCreateBy(loginUser.getUserId());
        orgManage.setUpdateBy(loginUser.getUserId());
        orgManage.setCreateTime(LocalDateTime.now());
        orgManage.setUpdateTime(LocalDateTime.now());
        orgManageService.save(orgManage);
        removeById(id);
    }
}
